package org.hgvc.webapp.ets.jsf.beans.admin;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;

import org.hgvc.webapp.ets.bo.Agreement;
import org.hgvc.webapp.ets.bo.AgreementControl;
import org.hgvc.webapp.ets.bo.InvoiceDtl;
import org.hgvc.webapp.ets.bo.Member;
import org.hgvc.webapp.ets.bo.MemberPointEntitle;
import org.hgvc.webapp.ets.bo.MemberTransaction;
import org.hgvc.webapp.ets.bo.MicTask;
import org.hgvc.webapp.ets.constant.TerminateConstant;
import org.hgvc.webapp.ets.jsf.beans.finance.MemberInvListingBean;
import org.hgvc.webapp.ets.spring.service.MemberService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;
import org.jocsb.base.util.FacesUtils;


public class AdminMemberAgreementPointEnquiryBean extends AbstractBean {

	public static final String BEAN_NAME="adminMemberAgreementPointEnquiryBean";
	public static final String NAV_LIST="adminMemberViewAgreementPoint";
	public static final String TAB_TRANSACTION="tabTransaction";
	public static final String TAB_INVOICE="tabInvoice";
	public static final String TAB_Other="tabOther";
	public static final String NAV_ADVANC_FINANCE="navAdvanceFinance";
	 
	private MemberService service;
	private Agreement agreement;
	private List<MemberPointEntitle> pointList;
	private List<MemberTransaction> memberTransactionList;
	private List<MemberTransaction> singleEmptyList;
	private String currentTabStatus;
	private List<InvoiceDtl> historyInvoiceList;
	private Member member;
	private boolean userAccountSuspended;
	private List<MicTask> micTasks; 
	private boolean showAccrued;
	private boolean showAdjustment=false;
 	
 
	public boolean isShowAdjustment() {
		return showAdjustment;
	}

	public void setShowAdjustment(boolean showAdjustment) {
		this.showAdjustment = showAdjustment;
	}
	
	public String navAdvanceFinance(String actionNoP,String... params){
		MemberInvListingBean memberInvListingBean = (MemberInvListingBean)FacesUtils.getManagedBean(MemberInvListingBean.BEAN_NAME);
		memberInvListingBean.initFinanceRole();
		
		memberInvListingBean.setMemberSearch(params[2]);
		memberInvListingBean.listInvActionListener(params[1]);

		memberInvListingBean.searchMemberAction(null);
		memberInvListingBean.setActionNo(actionNoP);
		return memberInvListingBean.getInvLstAcOutcome();
	}
	
	public String advanceAction() throws Exception{
		service.doSavePreAdvanceEntitlement(agreement, getThisUser());
		return "";
	}
	
	public void accruedAction(ActionEvent actionEvent) throws Exception{
		UIParameter parameter = (UIParameter)actionEvent.getComponent().findComponent("selectedMemberPointEntitlement2");
		String id = (String)parameter.getValue();
		for(MemberPointEntitle c:pointList){
			if(c.getMemberPointEntitleId().equalsIgnoreCase(id)){
				try {
					service.doaAccruedDate(c, c.getAccruedDate() , getThisUser());
					showAdjustment = false;
					showAccrued= false;
					refreshHistory();
					
				} catch (BaseException e) { 
					initErrorBean(e);
				}
				break;
			}
		}
	}
	
	public void adjustAddAction(ActionEvent actionEvent) throws Exception{
		UIParameter parameter = (UIParameter)actionEvent.getComponent().findComponent("selectedMemberPointEntitlement");
		String id = (String)parameter.getValue();
		for(MemberPointEntitle c:pointList){
			if(c.getMemberPointEntitleId().equalsIgnoreCase(id)){
				try {
					service.doSaveMemberPointAdjustment(c, "+",  getThisUser() );
					showAdjustment = false;
					showAccrued= false;
					refreshHistory();
					
				} catch (BaseException e) { 
					initErrorBean(e);
				}
				break;
			}
		}
	}
	
	public void adjustMinusAction(ActionEvent actionEvent) throws Exception{
		UIParameter parameter = (UIParameter)actionEvent.getComponent().findComponent("selectedMemberPointEntitlement");
		String id = (String)parameter.getValue();
		for(MemberPointEntitle c:pointList){
			if(c.getMemberPointEntitleId().equalsIgnoreCase(id)){
				try {
					service.doSaveMemberPointAdjustment(c, "-" ,getThisUser());
					showAdjustment = false;
					showAccrued= false;
					refreshHistory();
					
				} catch (BaseException e) { 
					initErrorBean(e);
				}
				
				break;
			}
		}
		
	}
	
	public void adjustmentShowAction(ActionEvent actionEvent){
		showAdjustment = true;
		showAccrued= false;
	}
	
	public void accruedShowAction(ActionEvent actionEvent){
		showAdjustment = false;
		showAccrued= true;
	}
	 
	public void setPointList(List<MemberPointEntitle> pointList) {
		this.pointList = pointList;
	}
	public List<MicTask> getMicTasks() throws Exception {
		
		return service.getMicTask(member.getMemberId());
	}
	public void setMicTasks(List<MicTask> micTasks) {
		this.micTasks = micTasks;
	}
	private void refreshHistory() throws BaseException{
		List<MemberTransaction> memberTransactionList = service.getMemberTransactionList(agreement.getAgreementId());	
    	setMemberTransactionList(memberTransactionList);
    	
	}
	public boolean isUserAccountSuspended() {
		userAccountSuspended=false;
		if (member.getLoginUser().getStatus().equalsIgnoreCase(TerminateConstant.SUSPENDED))
			userAccountSuspended=true;
		
		return userAccountSuspended;
	}
	public Member getMember() {
		return member;
	}
	public void setMember(Member member) {
		this.member = member;
	}
	public List<InvoiceDtl> getHistoryInvoiceList() {
		return historyInvoiceList;
	}
	public void setHistoryInvoiceList(List<InvoiceDtl> historyInvoiceList) {
		this.historyInvoiceList = historyInvoiceList;
	}
	public List<MemberTransaction> getSingleEmptyList() {
		singleEmptyList = new ArrayList<MemberTransaction>();
		singleEmptyList.add(new MemberTransaction());
		return singleEmptyList;
	}
	public String getCurrentTabStatus() {
		if (currentTabStatus==null)
			currentTabStatus = TAB_TRANSACTION;
		return currentTabStatus;
	}
	public String getTabTransStatus(){
		return TAB_TRANSACTION;
	}
	public String getTabMicStatus(){
		return TAB_Other;
	}
	public String getTabInvoiceStatus(){
		return TAB_INVOICE;
	}
	public void tabTransListener(ActionEvent event){
		this.currentTabStatus=TAB_TRANSACTION;
	}
	public void tabInvoiceListener(ActionEvent event){
		this.currentTabStatus=TAB_INVOICE;
	}
	public void tabMicListener(ActionEvent event){
		this.currentTabStatus=TAB_Other;
	}
	
	public Agreement getAgreement() {
		return agreement;
	}

	public void setAgreement(Agreement agreement) {
		this.agreement = agreement;
	}

	public void setService(MemberService service) {
		this.service = service;
	}
	
	
	public List<MemberTransaction> getMemberTransactionList() {
		return memberTransactionList;
	}

	public void setMemberTransactionList(
			List<MemberTransaction> memberTransactionList) {
		this.memberTransactionList = memberTransactionList;
	}

	public List<MemberPointEntitle> getPointList() {
		try{
			pointList = service.getMemberPointEntitleList(agreement.getAgreementId());
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return pointList;
	}
	
	public String navListAction(Agreement agreement){
		this.agreement = agreement;
		return NAV_LIST.concat("_"+getRoleName());
	}

	public boolean getShowAccrued() {
		return showAccrued;
	}

	public void setShowAccrued(boolean showAccrued) {
		this.showAccrued = showAccrued;
	}
	
	
	
}